home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / mc302emb.zip / BYTESWAP.C < prev    next >
C/C++ Source or Header  |  1994-03-18  |  581b  |  28 lines

  1. /*
  2.  * Example of using a union to overlay int (16 bit) and char (8 bit)
  3.  * data. In this program, a 16 bit int is written to memory, and then
  4.  * the high and low nibbles are exchanged by reading them as chars
  5.  * defined in a union with the int.
  6.  */
  7.  
  8. #include cflea.h
  9.  
  10. main()
  11. {
  12.     union {
  13.         int Idata;
  14.         struct {
  15.             char byte1;
  16.             char byte2; } Bdata; } data;
  17.  
  18.     int i;
  19.     char t;
  20.  
  21.     for(i=0; i < 10; ++i) {
  22.         data.Idata = i;
  23.         t = data.Bdata.byte1;
  24.         data.Bdata.byte1 = data.Bdata.byte2;
  25.         data.Bdata.byte2 = t;
  26.         printf("%04x <-> %04x\n", i, data.Idata); }
  27. }
  28.